//
//  SearchHistoryDao.m
//  JiaoX
//
//  Created by yinqy on 15/1/8.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "SearchHistoryDao.h"
#import "FMDatabase.h"

#define TABLENAME @"TB_SEARCH"
#define KEYWORDS @"keywords"

@interface SearchHistoryDao()
{
    FMDatabase *db;
    NSString *database_path;
}

@end

@implementation SearchHistoryDao
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documents = [paths objectAtIndex:0];
        database_path = [documents stringByAppendingPathComponent:DB_NAME];
        db = [FMDatabase databaseWithPath:database_path];

        [self createTable];
    }
    return self;
}
/**
 *  创建表
 */
- (void)createTable{
    //sql 语句
    if ([db open]) {
        NSString *sql =  [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' (_id integer primary key autoincrement,'%@' varchar(100),key1 varchar(100),key2 varchar(100),key3 varchar(100),key4 varchar(100),key5 varchar(100))",TABLENAME,KEYWORDS];
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"error when creating db table");
        } else {
            NSLog(@"success to creating db table");
        }
        [db close];
    }
}

/**
 *  添加关键字
 *
 *  @param keyWords 关键字
 *
 *  @return 是否添加成功
 */
-(BOOL)addKeyWords:(NSString *)keyWords{
    
    if ([self isContainsKeyWords:keyWords]) {
        return YES;
    }
    if ([db open]) {
        NSString *sql= [NSString stringWithFormat:
                               @"INSERT INTO '%@' ('%@') VALUES ('%@')",
                               TABLENAME, KEYWORDS, keyWords];
        BOOL res = [db executeUpdate:sql];
        
        if (!res) {
            NSLog(@"error when insert db table");
        } else {
            NSLog(@"success to insert db table");
        }
        [db close];
        return res;
    }
    
    return NO;
}

-(BOOL)isContainsKeyWords:(NSString *)keyWords{
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@'",TABLENAME,KEYWORDS,keyWords];
        FMResultSet * rs = [db executeQuery:sql];
        if ([rs next]) {
            [db close];
            return YES;
        }
    }
    return NO;
}

/**
 *  删除所有
 *
 *  @return
 */
-(BOOL)deleteAllHistory{
    if ([db open]) {
        NSString *sql= [NSString stringWithFormat:
                               @"DELETE FROM '%@'",
                               TABLENAME];
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"error when delete db table");
        } else {
            NSLog(@"success to delete db table");
        }
        [db close];
        return res;
    }
    return NO;
}
/**
 *  查询所有数据
 *
 *  @return 结果array
 */
-(NSArray *)selectAllData{
    NSMutableArray *temp = [NSMutableArray array];
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@",TABLENAME];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString * keyWords = [rs stringForColumn:KEYWORDS];
            [temp addObject:keyWords];
        }
        [db close];
        return [NSArray arrayWithArray:temp];
    }
    return temp;
}

@end
